package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.entity.SysRole;
import com.example.service.ISysRightService;
import com.example.service.ISysRoleRightService;
import com.example.service.ISysRoleService;
import com.example.utils.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 控制器
 * </p>
 *
 * @ClassName SysRoleController
 * @since 2024-03-05
 */
@Controller
@RequestMapping("/role")
public class SysRoleController {

    @Autowired
    private ISysRoleService sysRoleService;

    @Autowired
    private ISysRightService sysRightService;
    @Autowired
    private ISysRoleRightService roleRightService;

    /**
     * 跳转新增页面
     */
    @RequestMapping(value = "/add")
    public Object toInsert() {
        return "/role/add";
    }

    /**
     * 增
     */
    @PostMapping(value = "/save")
    @ResponseBody
    public Object insert(SysRole sysRole) {
        if (null!=sysRole.getRoleId()){
            sysRoleService.updateById(sysRole);
        }else {
            sysRoleService.save(sysRole);
        }
        return "redirect:role/list";
    }

    /**
     * 删
     */
    @RequestMapping(value = "/del/{id}")
    @ResponseBody
    public Object delete(@PathVariable(name = "id") Long id) {
        return sysRoleService.removeById(id);
    }

    /**
     * 跳转修改页面
     */
//
    @RequestMapping(value = "/edit")
    public Object toUpdate(Integer roleId, Model model) {


        model.addAttribute("role", sysRoleService.getById(roleId));
        model.addAttribute("rights", sysRightService.list());
        List<String> roleRights=roleRightService.getRightByUserRoleId(Long.valueOf(roleId)).stream()
                .map(sysRight -> {
                    return  sysRight.getRightCode();
                }).collect(Collectors.toList());
                ;
        model.addAttribute("roleRights", roleRights);
        return "/role/edit";
    }

    /**
     * 改
     */
    @PutMapping(value = "/save")
    @ResponseBody
    public Object update(SysRole sysRole) {
        return sysRoleService.updateById(sysRole);
    }


    /**
     * 分页列表
     */
    @RequestMapping(value = "/list")
    public Object getUserPage(PageUtil page, SysRole sysRole, Model model) {

        PageUtil<SysRole> page1 = (PageUtil<SysRole>) sysRoleService.page(page, new QueryWrapper<>(sysRole));

//        PageUtil pageUtil = new PageUtil(page1);

        model.addAttribute("rolePage", page1);
        return "/role/list";
    }

//    @RequestMapping("/list")
//    public String list(@RequestParam(defaultValue = "1") int PageIndex,
//                       @RequestParam(defaultValue = "5") int PageSize,
//                       Model model){
//        //查询分页数据
//
//        Page page =new Page();
//        page.setCurrent(PageIndex);
//        page.setSize(PageSize);
//        page= sysRoleService.page(page);
//
//
//        model.addAttribute("rolePage",page);
//
//        return "role/list";
//
//    }

}
